/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Organization;
import Organization.DataLayer.OrganizationDataManager;
import DataModel.Organization;
import java.util.*;
/**
 *
 * @author Carlos
 */
public class OrganizationManager
{
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private HashMap <Integer, Organization> _organizations;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    private static OrganizationManager _instance = new OrganizationManager();

    private OrganizationManager()
    {
        getOrganizations();
    }

    public static OrganizationManager GetInstance()
    {
        return _instance;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Public Methods">
    public Organization GetUserOrganization(int userId)
    {
        Iterator <Organization> orgIt = _organizations.values().iterator();
        while(orgIt.hasNext())
        {
            Organization next = orgIt.next();
            if(next.hasResponsible(userId))
                return next;
            else
                orgIt.next();
        }
        return null;
    }

    public Organization GetOrganization(int organizationId)
    {
        //if(_organizations == null)
            getOrganizations();
        try
        {
            return _organizations.get(organizationId);
        }
        catch(Exception ex){ }
        return null;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Private Methods">

    private void getOrganizations()
    {//Se saco del constructor para evitar que se enciclara
        try
        {
            Organization[] organizations = OrganizationDataManager.GetInstance().GetOrganizations();
            _organizations = new HashMap<Integer, Organization>();
            for(int orgIndex = 0; orgIndex < organizations.length; orgIndex++)
                _organizations.put(organizations[orgIndex].getId(), organizations[orgIndex]);
        }
        catch(Exception ex){ }
    }

    // </editor-fold>

}
